/**
 * 询价.COM v1.0.0
 * http://xun-jia.com/
 *
 * Copyright (c) 2009 
 * 四川漾天空网络科技有限公司
 * 成都市天府软件园A3栋2楼
 *
 */
package com.xunjia.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xunjia.util.Constants;

/**
 * @author Mark
 * 
 * @Email Mark_yang@xun-jia.com
 * 
 * @Date 2009-4-7 上午08:50:22
 * 
 */
public class AuthenticateCodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 生成隨機數長度
	private static final int length = 4;
	// 驗證碼圖片寬
	private static final int width = 62;
	// 驗證碼圖片高
	private static final int height = 22;
	// 设置备选验证码:包括"a-z"和数字"0-9"
	private static final String[] base = { "a", "b", "c", "d", "e", "f", "g",
			"h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
			"u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6",
			"7", "8", "9" };

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 设置页面不缓存
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		// 创建内存图像
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		// 获取图形上下文
		Graphics g = image.createGraphics();
		// 设定图像背景色(因为是做背景，所以偏淡)
		g.setColor(getRandColor(180, 250));
		g.fillRect(0, 0, width, height);
		// 设置字体
		g.setFont(new Font("Times New Roman", Font.PLAIN, 17));
		// 设置随机种子
		Random rand = new Random();
		// 驗證碼數據庫長度
		int size = base.length;
		// 新建字符緩沖序列,主要用於存儲驗證碼
		StringBuffer str = new StringBuffer();
		// 生成驗證碼
		for (int i = 0; i < length; i++) {
			// 獲取下一個字符位置
			int start = rand.nextInt(size);
			// 獲取下一字符
			String tmpStr = base[start];
			// 添加至str
			str.append(tmpStr);
			// 生成随机颜色(因为是做前景，所以偏深)
			g.setColor(getRandColor(10, 150));
			// 将此字画到图片上
			g.drawString(tmpStr, 13 * i + 6 + rand.nextInt(5), 14 + rand
					.nextInt(6));
		}
		// 将认证码存入session
		request.getSession().setAttribute(Constants.AUTH_CODE, str.toString());
		// 图象生效
		g.dispose();
		// 獲取輸出流對象
		OutputStream out = response.getOutputStream();
		// 输出图象到页面
		ImageIO.write(image, "JPEG", out);
		// 刷新缓冲区
		out.flush();
		// 关闭输出流
		out.close();
	}

	/**
	 * 给定范围获得一个随机颜色
	 * 
	 * @param fc
	 *            前景色
	 * @param bc
	 *            背景色
	 * @return
	 */
	Color getRandColor(int fc, int bc) {
		Random random = new Random();
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}

}
